package com.lixl.controller;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;

import org.apache.commons.io.FileUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RequestPart;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

@Api(value = "文件操作", tags = { "文件上传", "文件下载" })
@Controller
@RequestMapping("file")
public class FileController {

	@ApiOperation(value = "上传文件")
	@ApiResponses(value = {
			@ApiResponse(code = 200, message = "successful operation", response = String.class),
			@ApiResponse(code = 400, message = "Invalid mobile supplied", response = String.class),
			@ApiResponse(code = 404, message = "server not available", response = String.class) })
	@RequestMapping(value = "upload", produces = { "application/json" }, method = RequestMethod.POST)
	@ResponseBody
	public boolean uploadFile(
			 @ApiParam(name="文件类型",value="图片上传",required=true,allowMultiple=true) @RequestPart(required=true) MultipartFile file) {
		InputStream source = null;
		try {
			if(file!=null){
				source = file.getInputStream();
				File destination = new File("D://temp/"+file.getName());
				FileUtils.copyInputStreamToFile(source, destination);
			}
		} catch (IOException e) {
			e.printStackTrace();
		}finally{
			if(source!=null){
				try {
					source.close();
				} catch (IOException e) {
				}
			}
		}
		return true;
	}
	
	@RequestMapping(value = "/upload.do")  
    public String upload(@RequestParam(value = "file", required = false) MultipartFile file){
		
		return "result";
	}

}
